package com.block.source.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.block.common.service.LogFactory;
import com.block.db.entity.SourceTaskInfo;
import com.block.db.mapper.SourceTaskInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.source.service.ISourceTaskInfoService;
import com.block.test.reqbean.source.CheckUserInfo;
import jnr.ffi.annotations.In;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 来源任务信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-05-21
 */
@Service
public class SourceTaskInfoServiceImpl extends ServiceImpl<SourceTaskInfoMapper, SourceTaskInfo> implements ISourceTaskInfoService {

    private static Logger log = LogFactory.getSourceLog();

    @Override
    public SourceTaskInfo getEnableEntityOne(Integer sourceId, CheckUserInfo checkUserInfo) {
        /**
         * 获取满足条件的 推送任务
         */
        List<SourceTaskInfo> taskInfoList = this.baseMapper.getEnableEntityList(sourceId);
        if (CollUtil.isEmpty(taskInfoList)){
            return null;
        }

        log.info("【线索任务过滤】用户资质信息【checkUserInfo】{}", JSON.toJSONString(checkUserInfo));
        SourceTaskInfo targetTaskInfo = null;
        for (SourceTaskInfo taskInfo : taskInfoList) {
            log.info("【线索任务过滤】任务信息【taskInfo】{}", JSON.toJSONString(taskInfo));

            boolean b = filterTaskInfo(taskInfo, checkUserInfo);
            if (b){
                targetTaskInfo = taskInfo;
                break;
            }
        }

        return targetTaskInfo;
    }

    private boolean filterTaskInfo(SourceTaskInfo sourceTaskInfo, CheckUserInfo checkUserInfo){
        String inCityArray = sourceTaskInfo.getInCityArray();
        if (StrUtil.isEmpty(inCityArray)){
            log.info("【线索任务过滤】城市信息为空");
            return false;
        }
        List<String> inCityList = JSON.parseArray(inCityArray, String.class);
        if (CollUtil.isEmpty(inCityList)){
            log.info("【线索任务过滤】城市信息为空列表");
            return false;
        }
        if (!inCityList.contains("999999") && !inCityList.contains(checkUserInfo.getCityCode())){
            // 不是全国
            log.info("【线索任务过滤】城市信息不满足条件");
            return false;
        }

        Integer minAge = sourceTaskInfo.getMinAge();
        Integer maxAge = sourceTaskInfo.getMaxAge();
        if (ObjectUtil.isNotEmpty(minAge) && Integer.valueOf(checkUserInfo.getAge()) < minAge){
            log.info("【线索任务过滤】年龄不符合条件minAge【minAge】{}【age】{}", minAge, checkUserInfo.getAge());
            return false;
        }
        if (ObjectUtil.isNotEmpty(maxAge) && Integer.valueOf(checkUserInfo.getAge()) > maxAge){
            log.info("【线索任务过滤】年龄不符合条件maxAge【maxAge】{}【age】{}", maxAge, checkUserInfo.getAge());
            return false;
        }

        String zhima = sourceTaskInfo.getZhima();
        if (StrUtil.isNotEmpty(zhima)){
            List<String> zhimaList = JSON.parseArray(zhima, String.class);
            if (!zhimaList.contains(checkUserInfo.getZhima())){
                log.info("【线索任务过滤】芝麻分不符合【zhimaFilter】{}【zhima】{}", zhima, checkUserInfo.getZhima());
                return false;
            }
        }
        return true;
    }
}
